package dk.dashboardFP.uiElements.scrollbar {
	import dk.dashboardFP.utils.Range;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.text.TextField;

	/**
	 * @author jk
	 */
	public class TextFieldScrollLink extends EventDispatcher implements IScrollLink {
		
		private var _oScrollbar : Scrollbar;
		private var _tfTarget : TextField;
		private var _rangeScrollSpaceRate : Range;

		public function TextFieldScrollLink(tfTarget : TextField) {
			_tfTarget = tfTarget;
			super();
			
			setup();
		}
		
		private function setup() : void {
			setBase();
			_tfTarget.addEventListener(Event.SCROLL, onScroll, false, 0, true);
		}

		public function get nScrollPosRate() : Number {
			return _rangeScrollSpaceRate.getRate(_tfTarget.scrollV);
		}
			
		
		private function onScroll(event : Event) : void {
			dispatchScrollChange();
		}
		
		private function dispatchScrollChange() : void {
			dispatchEvent(new ScrollLinkEvent(ScrollLinkEvent.SCROLL_CHANGE));
		}

		public function get nScrollSpaceFactor() : Number {
			return _tfTarget.numLines/(_tfTarget.maxScrollV+_tfTarget.numLines);
		}

		private function setBase() : void {
			_rangeScrollSpaceRate = new Range(1, _tfTarget.maxScrollV);
		}		
		
		public function registerScrollbar(oScrollbar : Scrollbar) : void {
			_oScrollbar = oScrollbar;
			_oScrollbar.addEventListener(ScrollBarEvent.SCROLL_RATE_CHANGED, onScrollRateChangeOnBar, false, 0, true);
		}
		
		private function onScrollRateChangeOnBar(event : ScrollBarEvent) : void {
			_tfTarget.scrollV=Math.round(_rangeScrollSpaceRate.getPosition(_oScrollbar.nScrollRage)); 
		}
		
		public function setInactive() : void {
		}
		
		public function setActive() : void {
		}
		
		public function get bIsActive() : Boolean {
			return true;
		}
	}
}
